// key-value 模型
public class HashBucket {
    private static class Node {
        private int key;
        private int value;
        Node next;

        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }


    private Node[] array;
    private int size;   // 当前的数据个数
    private static final double LOAD_FACTOR = 0.75;
    private static final int DEFAULT_SIZE = 10;//默认桶的大小

    public HashBucket() {
        this.array = new Node[DEFAULT_SIZE];
    }

    public int put(int key, int value) {
        // write code here
        int index = key % array.length;
        Node cur = array[index];
        Node node = new Node(key, value);
        if (cur == null) {
            array[index] = node;
        } else {
            while (cur.next != null) {
                if (cur.key == key) {
                    int oldValue = cur.value;
                    cur.value = value;
                    return oldValue;
                }
                cur = cur.next;
            }
            if (cur.key == key) {
                int oldValue = cur.value;
                cur.value = value;
                return oldValue;
            }
            cur.next = node;
        }
        size++;
        if (loadFactor() >= LOAD_FACTOR) {
            resize();
        }
        return -1;
    }

    //前插
    public void put1(int key, int value) {
        int index = key % array.length;

        Node cur = array[index];
        //先遍历一遍整体的链表 是否存在当前key
        while (cur != null) {
            if (cur.key == key) {
                cur.value = value;
                return;
            }
            cur = cur.next;
        }
        //没有这个key
        Node node = new Node(key, value);
        node.next = array[index];
        array[index] = node;
        size++;

        if (loadFactor() >= 0.75) {
            resize();
        }
    }

    private void resize() {
        // write code here
        Node[] newArray = new Node[array.length * 2];
        //遍历原来的数组   将所有的元素  “重新哈希” 到新数组中！
        for (int i = 0; i < array.length; i++) {

            Node cur = array[i];
            while (cur != null) {
                //尾插
                int newIndex = cur.key % newArray.length;
                Node tmp = newArray[newIndex];
                Node node = new Node(cur.key, cur.value);
                if (tmp == null) {
                    newArray[newIndex] = node;
                } else {
                    while (tmp.next != null) {
                        tmp = tmp.next;
                    }
                    tmp.next = node;
                }
                cur = cur.next;
            }
        }
        //再将新数组赋给原数组
        array = newArray;
    }

    //前插
    private void resize1() {
        Node[] tmpArr = new Node[array.length * 2];
        //遍历原来的数组 将所有的元素    《重新哈希》到新的数组当中
        for (int i = 0; i < array.length; i++) {
            Node cur = array[i];
            while (cur != null) {
                //记录当前节点的下个节点
                Node curNext = cur.next;
                int newIndex = cur.key % tmpArr.length;
                //头插
                cur.next = tmpArr[newIndex];
                tmpArr[newIndex] = cur;
                cur = curNext;
            }
        }
        array = tmpArr;
    }

    private double loadFactor() {
        return size * 1.0 / array.length;
    }

    public int get(int key) {
        // write code here
        int index = key % array.length;
        Node cur = array[index];
        while (cur != null) {
            if (cur.key == key) {
                return cur.value;
            }
            cur = cur.next;
        }
        return -1;
    }
}